package com.exam.controller;

import com.exam.entity.Examination;
import com.exam.entity.Student;
import com.exam.service.ExaminationService;
import com.exam.service.StudentService;
import com.exam.util.Encrypt;
import jxl.*;
import jxl.read.biff.BiffException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 导入Excel控制器
 */
@RestController
public class ImportExcelController {

    @Resource
    private StudentService studentService;

    @Resource
    private ExaminationService examinationService;

    @RequestMapping("/import")
    public Object importExcel(String op, MultipartFile excel, HttpServletRequest req) throws IOException, BiffException{
        if(op!=null && !"".equals(op)){
            if("importStuInfo".equals(op)){
                List<Student> list = doImportStu(excel,req);
                return studentService.doImportStu(list);
            }else if("importExamInfo".equals(op)){
                List<Examination> list = doImportExam(excel,req);
                return examinationService. doImportExam(list);
            }
        }

        return null;
    }

    private List<Student> doImportStu(MultipartFile file,HttpServletRequest request) throws IOException, BiffException{
        List<Student> list = new ArrayList<Student>();
        //得到文件输入流
        Workbook wk=Workbook.getWorkbook(file.getInputStream());
        //获取第一张Sheet表
        Sheet sheet=wk.getSheet(0);
        //获取总行数
        int rowNum=sheet.getRows();
        //从数据行开始迭代每一行
        for(int i=1;i<rowNum;i++){
            //使用实体类封装单元格数据
            Student stu=new Student();
            //getContents()获取单元格的内容，返回字符串数据。适用于字符型数据的单元格
            //getCell(column,row)，表示取得指定列指定行的单元格（Cell）
            stu.setSavator("/image/zanwu.jpg");
            stu.setSno(sheet.getCell(0, i).getContents());
            stu.setSname(sheet.getCell(1, i).getContents());
            stu.setSsex(sheet.getCell(2, i).getContents());
            stu.setSidentity(sheet.getCell(3, i).getContents());
            stu.setStel(sheet.getCell(4, i).getContents());
            stu.setSemail(sheet.getCell(5, i).getContents());
            stu.setSunivsersity(sheet.getCell(6, i).getContents());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = ((DateCell)sheet.getCell(7, i)).getDate();
            stu.setSbirth(sdf.format(date));
            stu.setSpwd(Encrypt.md5("123456"));
            list.add(stu);
        }
        wk.close();

        return list;
    }

    private List<Examination> doImportExam(MultipartFile file,HttpServletRequest request) throws IOException, BiffException{
        List<Examination> list = new ArrayList<Examination>();
        //得到文件输入流
        Workbook wk=Workbook.getWorkbook(file.getInputStream());
        //获取第一张Sheet表
        Sheet sheet=wk.getSheet(0);
        //获取总行数
        int rowNum=sheet.getRows();
        //从数据行开始迭代每一行
        for(int i=1;i<rowNum;i++){
            //使用实体类封装单元格数据
            Examination examination = new Examination();
            //getContents()获取单元格的内容，返回字符串数据。适用于字符型数据的单元格
            //getCell(column,row)，表示取得指定列指定行的单元格（Cell）
            examination.setEname(sheet.getCell(0, i).getContents());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            examination.setEstart(sdf.format(((DateCell)sheet.getCell(1, i)).getDate()));
            examination.setEend(sdf.format(((DateCell)sheet.getCell(2, i)).getDate()));
            examination.setEmaxnum(Integer.parseInt(sheet.getCell(3, i).getContents()));
            examination.setEcurrnum(Integer.parseInt(sheet.getCell(4, i).getContents()));
            examination.setOutofdate(0);
            examination.setEpic("/image/zanwu.jpg");
            list.add(examination);
        }
        wk.close();

        return list;
    }

    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    public void setExaminationService(ExaminationService examinationService) {
        this.examinationService = examinationService;
    }
}
